/*
 * JDNuevaPartida
 * 
 * Created on 6 de octubre de 2005, 0:07
 * 
 * Pollo Verde Software 2006
 * 
 * Este programa se distribuye segun la licencia GPL v.2 o posteriores y no
 * tiene garantias de ningun tipo. Puede obtener una copia de la licencia GPL o
 * ponerse en contacto con la Free Software Foundation en http://www.gnu.org
 */
package org.pvs.palitos.spgui;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import org.pvs.palitos.SuperPalitos;

/**
 * @author Angel Luis Calvo Ortega
 */
public class NuevoJuegoDialog extends JDialog {
  private static final long serialVersionUID = 5683628743261965310L;
  private ButtonGroup bGModo, bGNum, bGDificultad;
  private JRadioButton jRBPachanga, jRBCrono, jRB1p, jRB2p, jRBFacil, jRBNormal, jRBDificil;
  private JPanel jPanel;
  private JButton jBAccept, jBCancel;
  
  /**
   * Creates a new instance of JDNuevaPartida 
   */
  public NuevoJuegoDialog(Frame owner) {
    super(owner, "Nueva Partida", true);
    
    getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);

    // Modo -----------------------------------------------------------------
    /*jPanel = new JPanel(new GridLayout(2, 1));
    jPanel.setBorder(new TitledBorder(" Modo "));
    jRBPachanga = new JRadioButton("Pachanga");
    jRBPachanga.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      	jRBModoPachanga_actionPerformed();
      }
    });
    jRBPachanga.setSelected(true);
    jPanel.add(jRBPachanga);
    jRBCrono = new JRadioButton("ContraReloj");
    jRBCrono.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
      	jRBModoContrareloj_actionPerformed();
      }
    });
    jPanel.add(jRBCrono);
    getContentPane().add(jPanel);*/
    
    // Num--------------------------------------------------------------------
    jPanel = new JPanel(new GridLayout(2, 1));
    jPanel.setBorder(new TitledBorder(" Numero de jugadores "));
    jRB1p = new JRadioButton("1 Jugador");
    jRB1p.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jRBNum1J_actionPerformed();
      }
    });
    jRB1p.setSelected(true);
    jPanel.add(jRB1p);
    jRB2p = new JRadioButton("2 Jugadores");
    jRB2p.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jRBNum2J_actionPerformed();
      }
    });
    jPanel.add(jRB2p);
    getContentPane().add(jPanel);
    
    // Dificultad------------------------------------------------------------
    jPanel = new JPanel(new GridLayout(3, 1));
    jPanel.setBorder(new TitledBorder(" Dificultad "));
    jRBFacil = new JRadioButton("Facil");
    jPanel.add(jRBFacil);
    jRBNormal = new JRadioButton("Normal");
    jRBNormal.setSelected(true);
    jPanel.add(jRBNormal);
    jRBDificil = new JRadioButton("Dificil");
    jPanel.add(jRBDificil);
    getContentPane().add(jPanel);
       
    // Grupos----------------------------------------------------------------
    bGModo = new ButtonGroup();
    bGModo.add(jRBPachanga);
    bGModo.add(jRBCrono);
    
    bGNum = new ButtonGroup();
    bGNum.add(jRB1p);
    bGNum.add(jRB2p);
    
    bGDificultad = new ButtonGroup();
    bGDificultad.add(jRBFacil);
    bGDificultad.add(jRBNormal);
    bGDificultad.add(jRBDificil);
    
    // Botones---------------------------------------------------------------
    jPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jBCancel = new JButton("Cancelar", SPFrame.II_CANCEL);
    jBCancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cancel_ActionPerformed(e);
      }
    });
    jPanel.add(jBCancel);
    jBAccept = new JButton("Aceptar", SPFrame.II_ACCEPT);
    jBAccept.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        accept_ActionPerformed(e);
      }
    });
    jPanel.add(jBAccept);
    getContentPane().add(jPanel, BorderLayout.SOUTH);
    
    pack();
    setLocationRelativeTo(owner);
  }
  
  protected void jRBModoPachanga_actionPerformed() {
  	setNum(true);
  	setDificultad(jRB1p.isSelected());
	}
  
  protected void jRBModoContrareloj_actionPerformed() {
  	setNum(false);
  	setDificultad(false);
	}
  
  protected void jRBNum1J_actionPerformed() {
  	setDificultad(true);
	}
  
  protected void jRBNum2J_actionPerformed() {
  	setDificultad(false);
	}
    
	private void setNum(boolean enabled) {
  	jRB1p.setEnabled(enabled);
  	jRB2p.setEnabled(enabled);
	}
	
	private void setDificultad(boolean enabled) {
    jRBFacil.setEnabled(enabled);
    jRBNormal.setEnabled(enabled);
    jRBDificil.setEnabled(enabled);
	}
	
  private void cancel_ActionPerformed(ActionEvent e) {
    dispose();
  }
  
  private void accept_ActionPerformed(ActionEvent e) {
    int modo;
    
    if(jRB1p.isSelected()) {
      if(jRBFacil.isSelected()) {
        modo = SuperPalitos.JUEGO_1J_FACIL;
      } else if(jRBNormal.isSelected()) {
        modo = SuperPalitos.JUEGO_1J_NORMAL;
      } else {
        modo = SuperPalitos.JUEGO_1J_DIFICIL;
      }
    } else {
      modo = SuperPalitos.JUEGO_2J;
    }
    SuperPalitos.getInstance().nuevaPartida(modo, null, true);
    dispose();
  }
}
